using System.Linq;
using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class GIVEN_source_that_is_not_parsed_WHEN_parsed {
        [Test]
        public void THEN_stop_parsing_and_add_production_name_to_error_log () {
            var terminal = IdentifierKeywordProduction.CreateTerminal("c");
            var sequence = new NonTerminal ("name",
                                            new SequenceRule (new KeywordRule ("a"),
                                                              new KeywordRule ("b"),
                                                              new LiteralRule(terminal)));

            try {
                sequence.ParseTerm ("a b d");
                Assert.That (() => { }, Throws.TypeOf (typeof (SyntaxException)));
            } catch (SyntaxException se) {
                Assert.That (se.SyntaxErrors.Count (), Is.EqualTo (1));
                SyntaxError syntaxError = se.SyntaxErrors.ElementAt(0);
                Assert.That (syntaxError.Index, Is.EqualTo (4));
                Assert.That (syntaxError.ExpectedProductions, Is.EquivalentTo (new [] {terminal.TerminalProduction.ErrorInformation}));
            }
        }
    }
}